define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    $.widget('rk.topicOperationCtrl',{
        _initValue:"",
        _initJson:null,
        _add_contents:null,
        _create:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._listeners();
        },
        _loadType:"search",
        _listeners:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            elem.on("click", "[act='topic_add_button']", function(){
                var text = elem.find("[act='topic_add_text']").val();
                if(text){
                    if(me._loadType == "add"){
                        var addTopc = function(){
                            rk.post("/json/oa_topic/add.action", {topicContent: text}, function (json) {
                                if (json.status === 0) {
                                    var temp = require("oa/tmpl/topic/tpl_topic_add_tags.tpl");
                                    var html = rk.templateText(temp, {content: json.topicContent, id: json.topicId});
                                    elem.find("[act='topic_tags_list']").append(html);
                                    elem.find("[act='topic_add_text']").val("");
                                    var topicIds = elem.find("[act='topic_topicForm_topicId']").val();
                                    if (topicIds) {
                                        topicIds += "," + json.topicId;
                                    } else {
                                        topicIds = json.topicId + "";
                                    }
                                    elem.find("[act='topic_topicForm_topicId']").val(topicIds);
                                    me._add_contents[json.topicContent] = json.topicContent;
                                } else {
                                    rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                                }
                            });
                        };
                        if(!me._add_contents) {
                            me._add_contents = {};
                            addTopc();
                        }else{
                            if(me._add_contents[text]){
                                rk.noticeError(rk.i18n('TOPIC_2'));
                            }else{
                                addTopc();
                            }
                        }

                    }else if(me._loadType == "update"){//opt
                        rk.post("/json/oa_feedTopic/add.action",{topicContent:text,systemId:opt.systemId,systemItemId:opt.systemItemId},function(json){
                            if(json.status === 0){
                                var temp = require("oa/tmpl/topic/tpl_topic_update_tags.tpl");
                                var html = rk.templateText(temp,{content:json.topicContent,id:json.topicId,feedTopicId:json.feedTopicId});
                                elem.find("[act='topic_tags_list']").append(html);
                                elem.find("[act='topic_add_text']").val("");
                            }else if(json.status === 221002){
                                rk.noticeError(rk.i18n('TOPIC_1'));
                            }else{
                                rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                            }
                        });
                    }
                }else{
                    rk.noticeError(rk.i18n('TOPIC_3'));
                }
            });
            elem.on("click", "[act='topic_delete']", function(){
                var button = $(this);
                if(me._loadType == "add"){
                    var topicIds = elem.find("[act='topic_topicForm_topicId']").val();
                    button.parent().remove();
                    var topicId = button.attr("topicId");
                    if(topicIds.indexOf(topicId+",")){
                        topicIds = topicIds.replace(topicId+",");
                    }else if(topicIds.indexOf(","+topicId)){
                        topicIds = topicIds.replace(","+topicId);
                    }
                    elem.find("[act='topic_topicForm_topicId']").val(topicIds);
                    var content = button.attr("content");
                    if(me._add_contents[content]){
                        delete me._add_contents[content];
                    }
                }else if(me._loadType == "update"){
                    var feedTopicId = button.attr("feedTopicId");
                    rk.post("/json/oa_feedTopic/del.action",{feedTopicId:feedTopicId},function(json){
                        if(json.status === 0){
                            button.parent().remove();
                        }else{
                            rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                        }
                    });
                }
            });
            if(opt.successButton){
                $(opt.successButton).click(function(){
                    me.loaddata("success");
                    $(opt.successButton).hide();
                    if(opt.updateButton){opt.updateButton.show();}
                });
            }
            if(opt.updateButton){
                $(opt.updateButton).click(function(){
                    me.loaddata("update");
                    $(opt.updateButton).hide();
                    if(opt.successButton){opt.successButton.show();}
                });
            }
        },
        _init:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me.loaddata(opt.type);
        },
        loaddata:function(type){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if(type == "add"){
                me._loadType = "add";
                var temp = require("oa/tmpl/topic/tpl_topic_update.tpl");
                elem.html(rk.templateText(temp));
            }else if(type == "update"){
                me._loadType = "update";
                if(elem.find("[act='topic_add_divs']").length>0) {
                    elem.find("[act='topic_add_divs']").show();
                }
                if(elem.find("[act='topic_delete']").length>0) {
                    elem.find("[act='topic_delete']").show();
                }
                if(elem.find("[act='topic_no_tag']").length>0){
                    elem.find("[act='topic_no_tag']").hide();
                }
            }else if(type=="search"){
                me._loadType = "search";
                //查询
                var temp = require("oa/tmpl/topic/tpl_topic_search.tpl");
                var param = {systemId:opt.systemId,systemItemId:opt.systemItemId};
                if(me.param){
                    param = me.param;
                }else{
                    me.param = param;
                }
                rk.post("/json/oa_feedTopic/feedTopicList.action", param, function (json) {
                    if (json.status === 0) {
                        me._initJson = json;
                        if (json.feedTopics.length > 0) {
                            var html = rk.templateText(temp, json);
                            elem.html(html);
                        } else {
                            var html = require("oa/tmpl/topic/tpl_topic_none.tpl");
                            elem.html(rk.templateText(html));
                        }
                        if(opt.success){
                            opt.success();
                        }
                    } else {
                        if(opt.error){
                            opt.error();
                        }
                        rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                    }
                });
            }else if(type=="success"){
                me._loadType = "success";
                if(elem.find("[act='topic_add_divs']").length>0) {
                    elem.find("[act='topic_add_divs']").hide();
                }
                if(elem.find("[act='topic_delete']").length>0) {
                    elem.find("[act='topic_delete']").hide();
                }
                if(elem.find("[act='topic_tags_list']") && elem.find("[act='topic_tags_list']").find(">li").length>0){
                    if(elem.find("[act='topic_no_tag']")){
                        elem.find("[act='topic_no_tag']").hide();
                    }
                }else{
                    if(elem.find("[act='topic_no_tag']").length>0){
                        elem.find("[act='topic_no_tag']").show();
                    }else{
                        var html = require("oa/tmpl/topic/tpl_topic_none.tpl");
                        elem.html(rk.templateText(html));
                    }
                }
            }
        },
        save:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
        }
    });
});